﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telerik.Web.Mvc.UI;
using KulaQ.Models.Entities;

namespace KulaQ.Models
{
    public class SurveyModel
    {
        public List<SurveyTree> SurveyTrees { get; set; }
        public int SelectedID { get; set; }

        KulaQDB kulaQDB = new KulaQDB();

        void Initialize()
        {
            SurveyTrees = new List<SurveyTree>();
        }

        public SurveyModel()
        {
            Initialize();
            foreach (var survey in kulaQDB.Surveys)
            {
                SurveyTrees.Add(new SurveyTree(survey.SurveyID));
            }
            SelectedID = SurveyTrees.FirstOrDefault().ID;
        }
    }

    public class SurveyTree
    {
        public int ID { get; set; }
        public string Name { get; set; }

        public SurveyTreeNode RootNode { get; set; }

        public List<SurveyTreeNode> Questions { get; set; }
        public List<SurveyTreeNode> Answers { get; set; }

        KulaQDB kulaQDB = new KulaQDB();

        void Initialize()
        {
            Questions = new List<SurveyTreeNode>();
            Answers = new List<SurveyTreeNode>();
        }

        public SurveyTree()
        {
            Initialize();
            RootNode = new SurveyTreeNode();
        }

        public SurveyTree(int SurveyID)
        {
            Initialize();

            ID = SurveyID;

            Name = kulaQDB.Surveys.FirstOrDefault(a => a.SurveyID == ID).SurveyName;

            foreach (var question in (from q in kulaQDB.Questions
                                      where q.SurveyID == ID
                                      orderby q.QuestionID
                                      select q))
            {
                var node = new SurveyTreeNode();
                node.Text = question.QuestionText;
                node.ID = question.QuestionID;
                Questions.Add(node);
                if (RootNode == null)
                {
                    RootNode = node;
                }
            }

            foreach (var answer in (from q in Questions
                                    join a in kulaQDB.Answers
                                    on q.ID equals a.QuestionID
                                    select a))
            {
                var node = new SurveyTreeNode();
                node.Text = answer.AnswerText;
                node.ID = answer.AnswerID;
                node.ParentID = answer.QuestionID;
                node.ChildID = answer.NextQuestionID;
                Answers.Add(node);
            }

            foreach (var answer in Answers)
            {
                foreach (var question in Questions)
                {
                    if (answer.ParentID == question.ID)
                    {
                        question.Items.Add(answer);
                    }
                    if (answer.ChildID == question.ID)
                    {
                        answer.Items.Add(question);
                    }
                }
            }
        }

        public void ExpendAll()
        {
            Answers.ForEach(a => a.Expanded = true);
            Questions.ForEach(q => q.Expanded = true);
        }
    }
    public class SurveyTreeNode : TreeViewItem
    {
        public int? ID { get; set; }
        public int? ParentID { get; set; }
        public int? ChildID { get; set; }
    }
}